<?php

namespace App\controllers;

use App\BaseController;
use App\models\SysMenu;
use System\Core\Request;

class SysController extends BaseController
{
    public function index()
    {
        $request = new Request();
        if ($request->isPost()) {
            $result = [];
            $menu =  new SysMenu();
            $res = $menu->all();
            if (!empty($res)) {
                foreach ($res as $key => $value) {
                    $result[] = [
                        'id' => $value['sysm_id'],
                        'parent' => $value['sysm_parent_id'],
                        'name' => $value['sysm_name'],
                        'icon' => $value['sysm_icon'],
                        'path' => $value['sysm_path'],
                        'sort' => $value['sysm_sort'],
                    ];
                }
            }
            $this->success('', $result, ['count' => count($result)],0);
        } else {
            return $this->view('admin/sys/index');
        }
    }

    public function add()
    {
        $request = new Request();
        if ($request->isPost()) {
            $data = $request->post();

            $parent = $data['parent'];
            $menuname = $data['menuname'];
            if (empty($menuname)) {
                $this->error('菜单名不能为空');
            }
            $path =  $data['path'];
            $sort =  $data['sort'];
            $icon =  $data['icon'];

            $sysMenu =  new SysMenu();
            try {
                $res = $sysMenu->create([
                    'sysm_parent_id' => $parent,
                    'sysm_name' => $menuname,
                    'sysm_path' => $path,
                    'sysm_sort' => $sort,
                    'sysm_icon' => $icon,
                ]);
                if(!$res){
                    $this->error('添加失败');
                }
                $this->success('添加成功');
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
        } else {
            $sysMenu =  new SysMenu();
            $sysMenuList = $sysMenu->all();

            return $this->view('admin/sys/add',[
                'sysMenuList' => $sysMenuList,
            ]);
        }
    }

    public function edit()
    {
        $request = new Request();
        if ($request->isPost()) {
            $data = $request->post();
            // print_r($data);
            $id =  $data['id'];
            $parent = $data['parent'];
            $menuname = $data['menuname'];
            if (empty($menuname)) {
                $this->error('菜单名不能为空');
            }
            $path =  $data['path'];
            $sort =  $data['sort'];
            // var_dump($data);exit;
            $sysMenu =  new SysMenu();
            try {
                $res = $sysMenu->update([
                    'sysm_parent_id' => $parent,
                    'sysm_name' => $menuname,
                    'sysm_path' => $path,
                    'sysm_sort' => $sort,
                ], ['sysm_id' => $id]);

                if(!$res){
                    $this->error('修改失败');
                }
                $this->success('修改成功');
                
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
        } else {
            $id = $request->get('id');
            $sysMenu =  new SysMenu();
            $sysMenuItem = $sysMenu->find($id,'sysm_id');
            $sysMenuList = $sysMenu->all();

            $parent_id = $sysMenuItem['sysm_parent_id'];
            if($sysMenuItem['sysm_parent_id']!=0){
                $sysParentInfo = array_filter($sysMenuList, function ($item) use ($parent_id) {
                    return $item['sysm_id'] == $parent_id;
                })[0];
            }
            return $this->view('admin/sys/edit', [
                'sysItem' => $sysMenuItem,
                'sysMenuList'=>$sysMenuList,
                'sysParentInfo'=>$sysParentInfo??[]
            ]);
        }
    }

    public function del($id){
        $sysMenu =  new SysMenu();
        try {
            $res = $sysMenu->delete($id,'sysm_id');
            if(!$res){
                $this->error('删除失败');
            }
            $this->success('删除成功');
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
    }
}
